package FPlot;

import java.awt.Color;
import java.util.Vector;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartFrame;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.xy.StandardXYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class FPlot {
	JFreeChart Chart=null;
	Double maxy=0.0;
	Double miny=10000000.0;
	Integer index;
	
	public FPlot(String title){
		Chart=ChartFactory.createScatterPlot(title, "x", "y", null, PlotOrientation.VERTICAL, true,false, false);
		index=0;
	}
	
	public boolean Add(Vector<Double> x,Vector<Double> y, String title, Boolean ifscatter){
		if(x.isEmpty() || x.size()!=y.size() || title==null )
			return false;
		
		if(ifscatter){
			XYSeries dataseries=new XYSeries(title);
			int i;
			for(i=0;i<x.size();i++){
				dataseries.add(x.elementAt(i), y.elementAt(i));
				if(y.elementAt(i).compareTo(maxy)>0){
					maxy=y.elementAt(i);
				}
				if(y.elementAt(i).compareTo(miny)<0){
					miny=y.elementAt(i);
				}
			}
			XYSeriesCollection DataSet=new XYSeriesCollection();
			DataSet.addSeries(dataseries);
			Chart.getXYPlot().setDataset(index, DataSet);
			Chart.getXYPlot().setRenderer(index, new XYLineAndShapeRenderer(false,true));
		}else{
			XYSeries dataseries=new XYSeries(title);
			int i;
			for(i=0;i<x.size();i++){
				dataseries.add(x.elementAt(i), y.elementAt(i));
				if(y.elementAt(i).compareTo(maxy)>0){
					maxy=y.elementAt(i);
				}
				if(y.elementAt(i).compareTo(miny)<0){
					miny=y.elementAt(i);
				}
			}
			XYSeriesCollection DataSet=new XYSeriesCollection();
			DataSet.addSeries(dataseries);
			Chart.getXYPlot().setDataset(index, DataSet);
			Chart.getXYPlot().setRenderer(index, new StandardXYItemRenderer());
		}
		
		index++;
		
		return true;
	}
	
	public JFreeChart GetChart(){
		return this.Chart;
	}
	
	public void SetChart(JFreeChart newchart){
		this.Chart=newchart;
	}
	
	public void show(){
				
		Chart.getXYPlot().setBackgroundPaint(Color.BLACK);
		Chart.getXYPlot().getRangeAxis().setAutoRange(false);
		Chart.getXYPlot().getRangeAxis().setRange(miny-1.0, maxy+1.0);
		
		ChartFrame CFrame=new ChartFrame("FPlot",Chart);
		
		CFrame.pack();
		CFrame.setVisible(true);
	}
	
	
}
